/**
 * 调试配置管理
 * 根据当前环境自动配置调试参数
 */

import getEnv from './env';

/**
 * 调试服务器配置接口
 */
interface DebugConfig {
  enabled: boolean;
  serverUrl: string;
  useWebSocket: boolean;
  connectionMode: 'usb' | 'lan' | 'disabled';
  reconnectInterval: number;
  maxReconnectAttempts: number;
  connectionTimeout: number;
  heartbeatInterval: number;
}

/**
 * 各环境的调试配置映射
 */
const DEBUG_CONFIGS: Record<string, DebugConfig> = {
  dev: {
    enabled: true,
    serverUrl: 'http://localhost:3000', // USB调试模式
    useWebSocket: true,
    connectionMode: 'usb',
    reconnectInterval: 3000,
    maxReconnectAttempts: 3,
    connectionTimeout: 5000,
    heartbeatInterval: 30000,
  },
  
  test: {
    enabled: true,
    serverUrl: 'http://localhost:3000', // USB调试模式
    useWebSocket: true,
    connectionMode: 'usb',
    reconnectInterval: 3000,
    maxReconnectAttempts: 3,
    connectionTimeout: 5000,
    heartbeatInterval: 30000,
  },
  
  sandbox: {
    enabled: true,
    serverUrl: 'http://10.240.20.130:3000', // 局域网调试模式
    useWebSocket: true,
    connectionMode: 'lan',
    reconnectInterval: 5000,
    maxReconnectAttempts: 2,
    connectionTimeout: 8000,
    heartbeatInterval: 30000,
  },
  
  staging: {
    enabled: false, // 预发布环境禁用调试
    serverUrl: '',
    useWebSocket: false,
    connectionMode: 'disabled',
    reconnectInterval: 0,
    maxReconnectAttempts: 0,
    connectionTimeout: 0,
    heartbeatInterval: 0,
  },
  
  prod: {
    enabled: false, // 生产环境禁用调试
    serverUrl: '',
    useWebSocket: false,
    connectionMode: 'disabled',
    reconnectInterval: 0,
    maxReconnectAttempts: 0,
    connectionTimeout: 0,
    heartbeatInterval: 0,
  },
};

/**
 * 默认调试配置（兜底配置）
 */
const DEFAULT_DEBUG_CONFIG: DebugConfig = {
  enabled: false,
  serverUrl: 'http://localhost:3000',
  useWebSocket: true,
  connectionMode: 'usb',
  reconnectInterval: 3000,
  maxReconnectAttempts: 3,
  connectionTimeout: 5000,
  heartbeatInterval: 30000,
};

/**
 * 获取当前环境的调试配置
 * @returns 调试配置对象
 */
export const getDebugConfig = (): DebugConfig => {
  try {
    const env = getEnv();
    const config = DEBUG_CONFIGS[env.ENVIRONMENT] || DEFAULT_DEBUG_CONFIG;
    
    // 在开发环境下，还要检查 __DEV__ 标志
    const finalConfig = {
      ...config,
      enabled: config.enabled && (typeof __DEV__ !== 'undefined' ? __DEV__ : true),
    };
    
    console.log(`🔧 调试配置: ${env.ENVIRONMENT} (${finalConfig.connectionMode}) - ${finalConfig.enabled ? '启用' : '禁用'}`);
    
    return finalConfig;
  } catch (error) {
    console.warn('获取调试配置失败，使用默认配置:', error.message);
    return DEFAULT_DEBUG_CONFIG;
  }
};

/**
 * 检查当前环境是否启用调试
 * @returns 是否启用调试
 */
export const isDebugEnabled = (): boolean => {
  return getDebugConfig().enabled;
};

/**
 * 获取调试服务器URL
 * @returns 调试服务器URL
 */
export const getDebugServerUrl = (): string => {
  return getDebugConfig().serverUrl;
};

/**
 * 获取调试连接模式
 * @returns 连接模式
 */
export const getDebugConnectionMode = (): 'usb' | 'lan' | 'disabled' => {
  return getDebugConfig().connectionMode;
};

/**
 * 导出配置接口供TypeScript使用
 */
export type { DebugConfig };

